<?php

class Soul_Db_Sql_Insert extends Soul_Db_Sql_Base{

	private $_values = array();
	private $_table = '';
	
	public function insert($table){
		$this->_table = $table;
        return $this;
	}

	public function set($field, $value, $type = 's'){
		$pn = 'p'.count($this->parameters);
		$this->_values[] = array(
			'name'=>$this->addQualifier($field), 
			'parameter'=>':'.$pn.':'.$type
		);
		$this->addParameter($pn, $value);
		return $this;
	}

	public function reset(){
		$this->_table ='';
		$this->_values = array();
		$this->parameters = array();
        return $this;
	}
	
	public function resetValue(){
		$this->_values = array();
		return $this;
	}	

	public function toSql(){
        $fields = array();
		$values = array();
		foreach($this->_values as $item){
			$values[] = $item['parameter'];
            $fields[] = $this->addQualifier($item['name']);
		}
		return 'INSERT `'.$this->_table.'`'.' ('.implode(',', $fields).') VALUES('.implode(',', $values).')';
	}

	public function execute($driver = NULL){
		$sql = $this->toSql();
        $driver = is_object($driver) ? $driver : Soul_Db_Factory::getDriver($driver);
        return $driver->setSql($sql, $this->getParameters())->execute();        
	}
}